package com.molyfun.parents.view.downloadStatusBtn;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.course.downloadObserver.CourseDownloadProgressCenter;
import com.molyfun.parents.impl.OnCourseDownloadTaskProgressUpdate;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.service.filedownload.CourseDownLoadUtils;
import com.molyfun.parents.utils.EmptyHelper;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/8/16.
 */
public class DownloadStateBar extends FrameLayout implements View.OnClickListener, OnCourseDownloadTaskProgressUpdate {

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.download_start)
    TextView mDownLoadStart;
    @InjectView(R.id.download_finish)
    TextView mDownLoadFinish;
    @InjectView(R.id.download_update)
    TextView mDownLoadUpdate;
    @InjectView(R.id.progress_bar)
    CircleProgressBar mCircleProgressBar;

    @Override
    public void onCourseDownloadStart(String courseDownloadId) {
        if(courseDownloadId.equals(mCourseBean.getDkey())){
            setDownloadState(DownLoadState.STATE_DOWNLOAD_PROCESS);
        }
    }

    @Override
    public void onCourseprogressUpdate(String courseDownloadId, int percent) {
        if(courseDownloadId.equals(mCourseBean.getDkey())){
            updateDownloadProgress(percent);
        }
    }

    @Override
    public void onCourseDownloadFinish(String courseDownloadId) {
        if(courseDownloadId.equals(mCourseBean.getDkey())){
            setDownloadState(DownLoadState.STATE_DOWNLOAD_FINISH);
        }
    }


    public interface DownLoadState{
        //never download before, show download button
        int STATE_NONE = 1001;
        //downloading, show update button
        int STATE_DOWNLOAD_PROCESS = 1002;
        //update download files
        int STATE_DOWNLOAD_UPDATE = 1003;
        //already download
        int STATE_DOWNLOAD_FINISH = 1004;
    }

    private int mCurrentDownloadState = DownLoadState.STATE_NONE;

    private CourseListResponse.CourseBean mCourseBean;

    public DownloadStateBar(Context context) {
        super(context);
        init(context, null);
    }

    public DownloadStateBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        init(context, attrs);
    }

    public DownloadStateBar(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    public void bindCourseData(CourseListResponse.CourseBean mCourseBean){
        this.mCourseBean = mCourseBean;
        if (CourseDownloadProgressCenter.getInstance().isCourseDownloadFinish(mCourseBean.getDkey())) {
            if(EmptyHelper.isTrue(mCourseBean.getIsupdate())){
                setDownloadState(DownLoadState.STATE_DOWNLOAD_UPDATE);
            }else{
                setDownloadState(DownloadStateBar.DownLoadState.STATE_DOWNLOAD_FINISH);
            }
        } else if (CourseDownloadProgressCenter.getInstance().isCourseDownloading(mCourseBean.getDkey())) {
            setDownloadState(DownloadStateBar.DownLoadState.STATE_DOWNLOAD_PROCESS);

        } else {
            setDownloadState(DownloadStateBar.DownLoadState.STATE_NONE);
        }
        CourseDownloadProgressCenter.getInstance().registeObserver(this);
    }

    private void init(Context context, AttributeSet attrs) {
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_download_bar, this);
        ButterKnife.inject(this);
        mDownLoadStart.setOnClickListener(this);
        mDownLoadUpdate.setOnClickListener(this);
        mCircleProgressBar.setOnClickListener(this);
    }

    public void setDownloadState(int state){
        this.mCurrentDownloadState = state;
        mDownLoadStart.setVisibility(View.GONE);
        mDownLoadFinish.setVisibility(View.GONE);
        mDownLoadUpdate.setVisibility(View.GONE);
        mCircleProgressBar.setVisibility(View.GONE);
        switch (state){
            case DownLoadState.STATE_NONE:
                mDownLoadStart.setVisibility(View.VISIBLE);
                break;
            case DownLoadState.STATE_DOWNLOAD_FINISH:
                mDownLoadFinish.setVisibility(View.VISIBLE);
                break;
            case DownLoadState.STATE_DOWNLOAD_PROCESS:
                mCircleProgressBar.setVisibility(View.VISIBLE);
                break;
            case DownLoadState.STATE_DOWNLOAD_UPDATE:
                mDownLoadUpdate.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.download_start:
            case R.id.download_update:
                mCircleProgressBar.setmSubCurProgress(0);
                startDownload();
                setDownloadState(DownLoadState.STATE_DOWNLOAD_PROCESS);
                break;
            case R.id.progress_bar:
//                CourseDownloadProgressCenter.getInstance().removeCourseDownloadState();
                //TODO 取消下载，通知service
//                setDownloadState(DownLoadState.STATE_NONE);
                //取消下载
                break;
        }
    }

    private void updateDownloadProgress(int progress){
        if(mCurrentDownloadState != DownLoadState.STATE_DOWNLOAD_PROCESS){
            setDownloadState(DownLoadState.STATE_DOWNLOAD_PROCESS);
        }
        mCircleProgressBar.setmSubCurProgress(progress);
    }

    public void startDownload(){
        mCourseBean.setRelid("1");
        CourseDownLoadUtils.startDownLoadCourse(getContext(), mCourseBean);
    }

}
